package com.wangshouyu.mp.pay.v3.common.enums;

import com.wangshouyu.mp.pay.v3.common.constant.UrlV3Constant;
import com.wangshouyu.mp.pay.v3.common.handler.TransactionsHandler;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author 王守钰
 * @description 交易类型枚举
 * @date 2020/8/14 10:01
 */
public enum TransactionsType implements TransactionsHandler {

    /**
     * APP
     */
    APP("APP","APP"){
        @Override
        public String url() {
            return UrlV3Constant.APP_TRANSACTIONS_URL;
        }

        @Override
        public String path() {
            try {
                return new URL(url()).getPath();
            } catch (MalformedURLException e) {
                throw new RuntimeException("获取请求路径异常!", e);
            }
        }

        @Override
        public String method() {
            return "POST";
        }
    },

    /**
     * JSAPI
     */
    JS_API("JSAPI","JSAPI") {
        @Override
        public String url() {
            return UrlV3Constant.JS_API_TRANSACTIONS_URL;
        }
        
        @Override
        public String path() {
            try {
                return new URL(url()).getPath();
            } catch (MalformedURLException e) {
                throw new RuntimeException("获取请求路径异常!", e);
            }
        }

        @Override
        public String method() {
            return "POST";
        }
    },

    /**
     * MICRO
     */
    MICRO("JSAPI","小程序支付") {
        @Override
        public String url() {
            return UrlV3Constant.JS_API_TRANSACTIONS_URL;
        }

        @Override
        public String path() {
            try {
                return new URL(url()).getPath();
            } catch (MalformedURLException e) {
                throw new RuntimeException("获取请求路径异常!", e);
            }
        }

        @Override
        public String method() {
            return "POST";
        }
    },

    /**
     * NATIVE
     */
    NATIVE("NATIVE","NATIVE") {
        @Override
        public String url() {
            return UrlV3Constant.NATIVE_TRANSACTIONS_URL;
        }

        @Override
        public String path() {
            try {
                return new URL(url()).getPath();
            } catch (MalformedURLException e) {
                throw new RuntimeException("获取请求路径异常!", e);
            }
        }

        @Override
        public String method() {
            return "POST";
        }
    },

    /**
     * H5
     */
    H5("H5","H5") {
        @Override
        public String url() {
            return UrlV3Constant.H5_TRANSACTIONS_URL;
        }

        @Override
        public String path() {
            try {
                return new URL(url()).getPath();
            } catch (MalformedURLException e) {
                throw new RuntimeException("获取请求路径异常!", e);
            }
        }

        @Override
        public String method() {
            return "POST";
        }
    },
    ;

    /**
     * 类型
     */
    private String type;

    /**
     * 描述
     */
    private String description;

    TransactionsType(String type, String description) {
        this.type = type;
        this.description = description;
    }


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
